package day03;

/*
 * Author: JacksonCoder
 * FileName: LambdaDemo
 * Version: 1.0.0
 * Date: 2025/4/10
 */

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;

public class LambdaDemo {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("banana","apple","pear","orange");
        // 传统写法 （匿名内部类）
        Collections.sort(list,new Comparator<String>(){
            @Override
            public int compare(String s1, String s2) {
                return s1.length() - s2.length();
            }
        });
        System.out.println(list);
        System.out.println("========");
        lambdaSort();
        System.out.println("========");
        foreachItem();
        System.out.println("========");
        RunnableDemo();
    }

    // lambda 写法 （更简洁）
    private static void lambdaSort(){
        List<String> list = Arrays.asList("banana","apple","pear","orange");

        list.sort((s1,s2)-> s1.length() - s2.length());

        System.out.println(list);
    }

    // 遍历每一个元素
    private static void foreachItem(){
        List<String> names = Arrays.asList("alen","Nexora","chris","Charlie","David");

        names.forEach(name -> System.out.println("Hello " + name));
    }

    // Runnable
    private static void RunnableDemo(){
        // 传统写法
        Runnable r = new Runnable(){
            @Override
            public void run() {
                System.out.println("Running...");
            }
        };
        new Thread(r).start();

        // Lambda 写法
        Runnable r1 = () -> System.out.println("Runnable of Lambda...");
        new Thread(r1).start();
    }

}
